// deviceInformation.js
const app = getApp()
Page({

  data: {
    deviecBrand: wx.getStorageSync('deviecBrand'),//请输入设备品牌
    machineryType: wx.getStorageSync('machineryType'), //机械类型（1-9对应挖掘机、装载机、矿用自卸车、推土机、压路机、平地机、混凝土机械、桩工机械、凿岩机械）
    dateOfProduction: wx.getStorageSync('dateOfProduction'),  //出厂日期

    array: ['', '挖掘机', '装载机', '矿用自卸车', '推土机', '压路机', '平地机', '混凝土机械', '桩工机械', '凿岩机械'],//picker选择项
    index: wx.getStorageSync('index'),//picker组件的角标

    identification: wx.getStorageSync('identification'), //所属的单位
    plateSubject: wx.getStorageSync('plateSubject'),//请输入设备型号
    machineryColour: wx.getStorageSync('machineryColour'),//请输入机械颜色
    engineSubject: wx.getStorageSync('engineSubject'),//请输入发动机型号
    maneuver: wx.getStorageSync('maneuver'),//行走方式
    power: wx.getStorageSync('power'),//请输入发动机功率
    resBuyDate: wx.getStorageSync('resBuyDate'),//购买日期
    enginePlateNum: wx.getStorageSync('enginePlateNum'),//请输入发动机编号
    productionAddress: wx.getStorageSync('productionAddress'),//请输入设备产地
    workTime: wx.getStorageSync('workTime'),//请输入设备工作小时
    weight: wx.getStorageSync('weight'),//请输入整机重量
    outputVolume: wx.getStorageSync('outputVolume'),//请输入尾气排放值
    factoryNum: wx.getStorageSync('factoryNum'),//出厂编号
    productionType: wx.getStorageSync('productionType'),//生产方式（0国产，1合资，2进口）
    fuel: wx.getStorageSync('fuel'), //燃料类型
    accept: false,
    isSubmit: true
  },

  //设备类型
  machineryType: function (e) {
    wx.setStorageSync('machineryType', e.detail.value)
    wx.setStorageSync('index', e.detail.value)
    this.setData({
      index: wx.getStorageSync('index'),
      machineryType: wx.getStorageSync('index')
    })
  },
  //设备品牌
  deviecBrand: function (e) {
    wx.setStorageSync('deviecBrand', e.detail.value)
    this.setData({
      deviecBrand: wx.getStorageSync('deviecBrand')
    })
  },
  //设备型号
  plateSubject: function (e) {
    wx.setStorageSync('plateSubject', e.detail.value)
    this.setData({
      plateSubject: wx.getStorageSync('plateSubject')
    })
  },
  //设备颜色
  machineryColour: function (e) {
    wx.setStorageSync('machineryColour', e.detail.value)
    this.setData({
      machineryColour: wx.getStorageSync('machineryColour')
    })
  },
  //发动机型号
  engineSubject: function (e) {
    wx.setStorageSync('engineSubject', e.detail.value)
    this.setData({
      engineSubject: wx.getStorageSync('engineSubject')
    })
  },
  //行走方式
  maneuver: function (e) {
    wx.setStorageSync('maneuver', e.detail.value)
    this.setData({
      maneuver: wx.getStorageSync('maneuver')
    })
  },
  //功率
  power: function (e) {
    var pattern = /^[0-9]*$/
    if (pattern.test(e.detail.value)) {
      wx.setStorageSync('power', e.detail.value)
      this.setData({
        power: wx.getStorageSync('power')
      })
    } else {
      wx.showToast({
        title: '请填入数字',
        duration: 2500,
        icon: 'loading',
        mask: true
      });
      wx.setStorageSync('power', '')
      this.setData({
        power: wx.getStorageSync('power')
      })
    }
  },
  //发动机编号
  enginePlateNum: function (e) {
    wx.setStorageSync('enginePlateNum', e.detail.value)
    this.setData({
      enginePlateNum: wx.getStorageSync('enginePlateNum')
    })
  },
  //生产地址
  productionAddress: function (e) {
    wx.setStorageSync('productionAddress', e.detail.value)
    this.setData({
      productionAddress: wx.getStorageSync('productionAddress')
    })
  },
  //工作时间
  workTime: function (e) {
    var pattern = /^[0-9]*$/
    if (pattern.test(e.detail.value)) {
      wx.setStorageSync('workTime', e.detail.value)
      this.setData({
        workTime: wx.getStorageSync('workTime')
      })
    } else {
      wx.showToast({
        title: '请填入数字',
        duration: 2500,
        icon: 'loading',
        mask: true
      });
      wx.setStorageSync('workTime', '')
      this.setData({
        workTime: wx.getStorageSync('workTime')
      })
    }
  },
  //设备重量
  weight: function (e) {
    var pattern = /^[0-9]*$/
    if (pattern.test(e.detail.value)) {
      wx.setStorageSync('weight', e.detail.value)
      this.setData({
        weight: wx.getStorageSync('weight')
      })
    } else {
      wx.showToast({
        title: '请填入数字',
        duration: 2500,
        icon: 'loading',
        mask: true
      });
      wx.setStorageSync('weight', '')
      this.setData({
        weight: wx.getStorageSync('weight')
      })
    }
  },
  //尾气排放值
  outputVolume: function (e) {
    wx.setStorageSync('outputVolume', e.detail.value)
    this.setData({
      outputVolume: wx.getStorageSync('outputVolume')
    })
  },
  //生产方式
  productionType: function (e) {
    wx.setStorageSync('productionType', e.detail.value)
    this.setData({
      productionType: wx.getStorageSync('productionType')
    })
  },
  //燃油类型
  fuel: function (e) {
    wx.setStorageSync('fuel', e.detail.value)
    this.setData({
      fuel: wx.getStorageSync('fuel')
    })
  },

  //是否同意协议
  accept: function (e) {
    if (e.detail.value == '0') {
      this.setData({
        accept: true
      })
    } else {
      this.setData({
        accept: false
      })
    }
  },
  //设备出场编号验证
  factoryNum: function (e) {
    var that = this;
    var factoryNum = e.detail.value
    if (factoryNum != '' && factoryNum != null) {
      wx.request({
        url: app.globalData.requestURL +'deviceInformation/' + factoryNum + '/getStatusByCondition',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == '0') {
            wx.setStorageSync('factoryNum', '')
            that.setData({
              accept: false,
              factoryNum: wx.getStorageSync('factoryNum')
            })
            wx.showToast({
              title: '该设备已注册',
              duration: 2500,
              icon: 'loading',
              mask: true
            });
          } else {
            wx.setStorageSync('factoryNum', factoryNum)
            that.setData({
              factoryNum: wx.getStorageSync('factoryNum')
            })
          }
        }
      })
    } else {
      setData({
        accept: false
      })
      wx.showToast({
        title: '出场编号错误',
        duration: 2500,
        icon: 'loading',
        mask: true
      });
    }
  },

  submit: function (e) {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      })
      var identification = that.data.identification;
      var deviecBrand = that.data.deviecBrand;
      if (deviecBrand == "" || deviecBrand == null) {
        wx.showToast({
          title: '设备品牌错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var machineryType = that.data.machineryType;
      if (machineryType == "" || machineryType == null) {
        wx.showToast({
          title: '设备类型错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var dateOfProduction = that.data.dateOfProduction;
      if (dateOfProduction == "" || dateOfProduction == null) {
        wx.showToast({
          title: '出场日期错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var plateSubject = that.data.plateSubject;
      if (plateSubject == "" || plateSubject == null) {
        wx.showToast({
          title: '型号错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var machineryColour = that.data.machineryColour;
      if (machineryColour == "" || machineryColour == null) {
        wx.showToast({
          title: '颜色错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var engineSubject = that.data.engineSubject;
      if (engineSubject == "" || engineSubject == null) {
        wx.showToast({
          title: '发动机型号错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var maneuver = that.data.maneuver;
      if (maneuver == "" || maneuver == null) {
        wx.showToast({
          title: '行走方式错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var power = that.data.power;
      if (power == "" || power == null) {
        wx.showToast({
          title: '功率错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var resBuyDate = that.data.resBuyDate;
      if (resBuyDate == "" || resBuyDate == null) {
        wx.showToast({
          title: '购买时间错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var enginePlateNum = that.data.enginePlateNum;
      if (enginePlateNum == "" || enginePlateNum == null) {
        wx.showToast({
          title: '发动机编号错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var productionAddress = that.data.productionAddress;
      if (productionAddress == "" || productionAddress == null) {
        wx.showToast({
          title: '设备产地错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var workTime = that.data.workTime;
      if (workTime == "" || workTime == null) {
        wx.showToast({
          title: '工作时间错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var weight = that.data.weight;
      if (weight == "" || weight == null) {
        wx.showToast({
          title: '设备重量错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var outputVolume = that.data.outputVolume;
      if (outputVolume == "" || outputVolume == null) {
        wx.showToast({
          title: '尾气值错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var factoryNum = that.data.factoryNum;
      if (factoryNum == "" || factoryNum == null) {
        wx.showToast({
          title: '出场编号错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var productionType = that.data.productionType;
      if (productionType == "" || productionType == null) {
        wx.showToast({
          title: '生产方式错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      var fuel = that.data.fuel;
      if (fuel == "" || fuel == null) {
        wx.showToast({
          title: '燃料类型错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        that.setData({
          accept: false
        })
        return false;
      }
      wx.request({
        url: app.globalData.requestURL + 'deviceInformation/save',
        data: {
          identification: identification,
          deviecBrand: deviecBrand,
          machineryType: machineryType,
          dateOfProduction: dateOfProduction,
          plateSubject: plateSubject,
          machineryColour: machineryColour,
          engineSubject: engineSubject,
          maneuver: maneuver,
          power: power,
          resBuyDate: resBuyDate,
          enginePlateNum: enginePlateNum,
          productionAddress: productionAddress,
          workTime: workTime,
          weight: weight,
          outputVolume: outputVolume,
          factoryNum: factoryNum,
          productionType: productionType,
          fuel: fuel
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: function (res) {
          if (res.data.status == '0') {
            //缓存设备出场编号
            wx.setStorageSync("factoryNum", factoryNum);
            wx.setStorageSync('deviecBrand', '');
            wx.setStorageSync('index', '');
            wx.setStorageSync('plateSubject', '');//请输入设备型号
            wx.setStorageSync('machineryColour', '');//请输入机械颜色
            wx.setStorageSync('engineSubject', '');//请输入发动机型号
            wx.setStorageSync('maneuver', '');//行走方式
            wx.setStorageSync('power', '');//请输入发动机功率
            wx.setStorageSync('dateOfProduction', '');
            wx.setStorageSync('resBuyDate', '');//购买日期
            wx.setStorageSync('enginePlateNum', '');//请输入发动机编号
            wx.setStorageSync('productionAddress', '');//请输入设备产地
            wx.setStorageSync('workTime', '');//请输入设备工作小时
            wx.setStorageSync('weight', '');//请输入整机重量
            wx.setStorageSync('outputVolume', '');//请输入尾气排放值
            wx.setStorageSync('productionType', '');//生产方式（0国产，1合资，2进口）
            wx.setStorageSync('fuel', ''); //燃料类型
            wx.showToast({
              title: '添加成功！',
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function () {
                wx.redirectTo({
                  url: '/page-deviceInformationImg/home'
                })
              }
            });
          } else {
            wx.showToast({
              title: '添加失败！',
              icon: 'loading',
              duration: 2000,
              mask: true,
              success: function () {
                that.setData({
                  isSubmit: true
                })
              }
            });
          }
        }
      })
    } else {
      wx.showToast({
        title: '不可多次提交',
        icon: 'loading',
        duration: 2000,
        mask: true
      });
    }
  },

  dateOfProduction: function (e) {
    wx.setStorageSync('dateOfProduction', e.detail.value)
    this.setData({
      dateOfProduction: wx.getStorageSync('dateOfProduction')
    })
  },
  resBuyDate: function (e) {
    wx.setStorageSync('resBuyDate', e.detail.value)
    this.setData({
      resBuyDate: wx.getStorageSync('resBuyDate')
    })
  },

  onLoad: function (options) {
    var that = this;
    console.log( wx.getStorageSync('index'));
    if (wx.getStorageSync('index') == '') {
      wx.setStorageSync('index', 1);
    }
    that.setData({
      identification: wx.getStorageSync('identification'),
      factoryNum: wx.getStorageSync('factoryNum'),
      machineryType: wx.getStorageSync('index'),
      index: wx.getStorageSync('index')
    })
    wx.setStorageSync('machineryType', wx.getStorageSync('index'));
  },



  onReady: function () {

  },

  onShow: function () {

  },


  onHide: function () {

  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {

  },


  onReachBottom: function () {

  },

  onShareAppMessage: function () {

  }
})